package com.pet.shop.controller.room;

import com.pet.config.Constants;
import com.pet.shop.dto.PageDto;
import com.pet.shop.entity.room.Hotel;
import com.pet.shop.entity.room.HotelTime;
import com.pet.shop.entity.shop.AdvertInfo;
import com.pet.shop.service.room.IHotelService;
import com.pet.shop.service.shop.IAdvertInfoService;
import com.pet.shop.utils.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("api/hotel")
@RestController
public class HotelApiController {

    @Autowired
    private IHotelService hotelService;
    @Autowired
    private IAdvertInfoService advertInfoService;
    @Value("${fileViewServer}")
    private String fileViewServer;
    @Value("${fileDefaultAdvert}")
    private String fileDefaultAdvert;
    @Value("${fileDefaultPic}")
    private String fileDefaultPic;

    /**
     * 酒店首页数据访问
     * @param pageDto
     * @return
     */
    @RequestMapping("index")
    public Map<String, Object> index(@RequestBody PageDto<Hotel> pageDto){
        //广告
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("type",Constants.HOTEL_TYPE);
        List<AdvertInfo> advertInfoList = advertInfoService.queryAdvertInfo(paramMap);
        advertInfoList.forEach(advertInfo -> {
            if(!StringUtils.isEmpty(advertInfo.getPicUrl()))
                advertInfo.setPicUrl(fileViewServer + advertInfo.getPicUrl());
            else{
                advertInfo.setPicUrl(fileViewServer + fileDefaultAdvert);
            }
        });

        //酒店首页推荐
        pageDto.getParams().put("status", 1);
        pageDto = hotelService.queryPage(pageDto);

        if(pageDto.getResults() == null) pageDto.setResults(new ArrayList<>());
        pageDto.getResults().forEach(hotel -> {
            if(!StringUtils.isEmpty(hotel.getPicUrl()))
                hotel.setPicUrl(fileViewServer + hotel.getPicUrl());
            else{
                hotel.setPicUrl(fileViewServer + fileDefaultPic);
            }
        });

        Map<String, Object> map = new HashMap<String, Object>();

        Map<String, Object> pageMap = new HashMap<String, Object>();
        pageMap.put("total", pageDto.getTotalRecord());
        pageMap.put("data", pageDto.getResults());

        map.put("code", HttpStatus.OK.value());
        map.put("hotel", pageMap);
        map.put("advert", advertInfoList);
        map.put("msg", "ok");
        return map;
    }

    /**
     * 分页查找
     * @param pageDto
     * @return
     */
    @RequestMapping("page")
    public Map<String, Object> queryPage(@RequestBody PageDto<Hotel> pageDto){
        pageDto.getParams().put("status", 1);
        pageDto = hotelService.queryPage(pageDto);
        if(pageDto.getResults() == null) pageDto.setResults(new ArrayList<>());
        pageDto.getResults().forEach(hotel -> {
            if(!StringUtils.isEmpty(hotel.getPicUrl()))
                hotel.setPicUrl(fileViewServer + hotel.getPicUrl());
            else{
                hotel.setPicUrl(fileViewServer + fileDefaultPic);
            }
        });
        return ApiResultUtil.success_page(pageDto);
    }

    /**
     * 返回可以使用的时间段
     * @param hotelId
     * @param date
     * @return
     */
    @RequestMapping("check")
    public Map<String, Object> check(Long hotelId, String date){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date datetime;
        try {
            datetime = sdf.parse(date);
        } catch (ParseException e) {
            return ApiResultUtil.error_param("日期格式不对，yyyy-MM-dd");
        }
        List<HotelTime> timeList = hotelService.queryHotelTime(hotelId, datetime);
        return ApiResultUtil.success_data(timeList);
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEntity(@PathVariable("id")Long id){
        Hotel hotel = hotelService.getEntity(id);
        if(hotel != null){
            if(!StringUtils.isEmpty(hotel.getPicUrl()))
                hotel.setPicUrl(fileViewServer + hotel.getPicUrl());
            else{
                hotel.setPicUrl(fileViewServer + fileDefaultPic);
            }
        }
        return ApiResultUtil.success_data(hotel);
    }
}
